package com.zhiban.project.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.zhiban.project.domain.question.QuestionData;

import dinamica.Db;
import dinamica.GenericTableManager;
import dinamica.Recordset;
import dinamica.RecordsetException;

/**
 * 试卷做题自动判卷
 * 
 * @author lxp
 *
 */
public class JudgeQuestionController extends GenericTableManager {

	public int service(Recordset inputParams) throws Throwable {
		HttpServletRequest request = this.getRequest();
		String exam_id = request.getParameter("exam_id");
		String ids[] = request.getParameterValues("id");
		String answers[] = request.getParameterValues("answer");
		Integer examType = getExamType(inputParams);
		// System.out.println("examType:"+examType);
		// System.out.println("id:"+ids);
		Map questionDataMap = getQuestionList(inputParams);
		List<QuestionData> questionDataList = new ArrayList<QuestionData>();
		Integer index = 0;
		double total_score = 0;
		boolean flag = false;
		if (ids != null)
			for (String id : ids) {
				String answer = answers[index++];
				answer = replaceAnswer(answer);
				QuestionData questionData = (QuestionData) questionDataMap
						.get(Integer.parseInt(id));
				QuestionData newQuestionData = new QuestionData();
				String right_answer = "";
				if (questionData.getQuestion_type_id() == 2) {// 多选
					right_answer = questionData.getAnswer();
					right_answer = replaceAnswer(right_answer);
				} else if (questionData.getQuestion_type_id() == 4) {// 填空
					right_answer = questionData.getAnswer();
					right_answer = replaceAnswer(right_answer);
				} /*
				 * else if (questionData.getQuestion_type_id() == 5) {//问答题
				 * questionData.setAnswer(answer); }
				 */
				if (replaceAnswer(answer).equals(
						replaceAnswer(questionData.getAnswer()))) {// 正确
					total_score = total_score + questionData.getPoints();
					newQuestionData.setPoints(questionData.getPoints());
				} else {
					newQuestionData.setPoints(0d);
				}
				newQuestionData.setId(questionData.getId());
				newQuestionData.setAnswer(answer);
				questionDataList.add(newQuestionData);
				flag = true;
			}
		if (flag) {
			String insert = getSQL(getResource("insert.sql"), inputParams);
			addUserExam(Integer.parseInt(exam_id), total_score, examType,
					insert, inputParams);
			String insert_answer = null;// getSQL(getResource("insert_answer.sql"),
										// inputParams);
			addUserQuestion(insert_answer, questionDataList);
		}
		int rc = 0;
		return rc;
	}

	private void addUserExam(Integer exam_paper_id, double score,
			Integer examType, String insert, Recordset inputParams)
			throws Throwable {
		// Recordset inputParams=new Recordset();
		inputParams.append("exam_paper_id", java.sql.Types.INTEGER);
		inputParams.append("point_get", java.sql.Types.DOUBLE);
		inputParams.append("is_subjective", java.sql.Types.INTEGER);
		inputParams.setValue("exam_paper_id", exam_paper_id);
		insert = insert.replace("${fld:point_get}", score + "");
		insert = insert.replace("${fld:is_subjective}", examType + "");
		getDb().exec(insert);
	}

	private void addUserQuestion(String insertSql,
			List<QuestionData> questionDataList) throws Throwable {
		Db db = getDb();
		Recordset detail = new Recordset();
		insertSql = getSQL(getResource("insert_answer.sql"), detail);
		detail.append("point_get", java.sql.Types.DOUBLE);
		detail.append("answer", java.sql.Types.VARCHAR);
		detail.append("question_id", java.sql.Types.INTEGER);
		for (QuestionData questionData : questionDataList) {
			detail.addNew();
			addQuestion(db, insertSql, detail, questionData);
			String cmd = getSQL(insertSql, detail);
			db.addBatchCommand(cmd);
		}
		db.exec();
	}

	private void addQuestion(Db db, String sql, Recordset detail,
			QuestionData question) throws RecordsetException {
		detail.setValue("point_get", question.getPoints());
		detail.setValue("answer", question.getAnswer());
		detail.setValue("question_id", question.getId());
	}

	private String replaceAnswer(String answer) {
		if (answer == null) {
			answer = "";
		}
		answer = answer.replace("#", ",");
		answer = answer.replace(" ", ",");
		answer = answer.replace("A", "1");
		answer = answer.replace("B", "2");
		answer = answer.replace("C", "3");
		answer = answer.replace("D", "4");
		return answer.toLowerCase();
	}

	/**
	 * 是否人工题
	 * 
	 * @param inputParams
	 * @return
	 * @throws Throwable
	 */
	@SuppressWarnings("unused")
	private Integer getExamType(Recordset inputParams) throws Throwable {
		Integer typeInteger = -1;
		String getrecord = getSQL(getResource("query-exam.sql"), inputParams);
		Recordset recordset = getDb().get(getrecord);
		if (recordset.next()) {
			typeInteger = recordset.getInteger("is_subjective");
		}
		return typeInteger;
	}

	private HashMap<Integer, QuestionData> getQuestionList(Recordset inputParams)
			throws Throwable {
		HashMap questionMap = new HashMap();
		String getrecord = getSQL(getResource("query-question-list.sql"),
				inputParams);
		Recordset recordset = getDb().get(getrecord);
		while (recordset.next()) {
			Integer id = recordset.getInteger("id");
			String name = recordset.getString("name");
			String answer = recordset.getString("answer");
			Integer question_type_id = recordset.getInteger("question_type_id");
			Double points = recordset.getDouble("points");
			QuestionData questionData = new QuestionData();
			questionData.setName(name);
			questionData.setId(id);
			questionData.setPoints(points);
			questionData.setAnswer(answer);
			questionData.setQuestion_type_id(question_type_id);
			questionMap.put(id, questionData);
		}
		return questionMap;
	}

}
